package com.gr.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gr.entity.AuctionRecord;
import com.gr.entity.User;
import com.gr.service.FeignService;
import com.gr.service.IAuctionRecordService;
import com.gr.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guoran
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/auctionRecord")
public class AuctionRecordController {
    @Autowired
    private FeignService fs;
    @Autowired
    private IAuctionRecordService ars;
    //根据藏品id查询拍卖信息
    @GetMapping("/selectByAuctionId/{id}")
    public List<AuctionRecord> selectByAuctionId(@PathVariable("id") int id){
        QueryWrapper<AuctionRecord> wrapper=new QueryWrapper<>();
        wrapper.eq("auction_id",id);
        wrapper.orderByDesc("record_price");//降序排列
        //1.return之前拿到用户远程过程调用user-module拿到用户id 用openfeign 2.前端ajax拿到
        List<AuctionRecord> list = ars.list(wrapper);
        //for循环list集合，record对象里面有userid，再远程调用用户模块，把用户对象查询出来，作为AuctionRecord类的属性
        for (AuctionRecord ar:list){
            Integer userId = ar.getUserId();
            User user = fs.selectOne(userId);
            ar.setUser(user);
        }
        return list;
    }

    @PostMapping("/outprice")
    public void outprice(@RequestHeader("authorization")String jwt,@RequestBody AuctionRecord ar){
        //验证是否出的是最高价
        ar.setRecordTime(LocalDateTime.now());
        DecodedJWT verify = JwtUtils.verify(jwt);
        String useridStr = verify.getClaim("userid").asString();
        Integer userid=Integer.parseInt(useridStr);
        ar.setUserId(userid);
        ars.checkAndSave(ar);
    }
}

